<?php
/**
 * Plugin Name: Destacados Widget
 * Plugin URI:  http://pampacomcdt.com.ar
 * Description: Muestra lo mas destacado de RosarioAlToque 
 * Author:      PampacomCDT
 * Version:     0.1
 * Author URI:  http://www.pampacomcdt.com.ar
 * Network:     true
 */

 
add_action( 'plugins_loaded', 'destacados_register_widgets' );
//add_action( 'wp_print_styles', 'enqueue_estilos' );


function destacados_register_widgets() {
	add_action('widgets_init', create_function('', 'return register_widget("Destacados_Widget");') );
}

function enqueue_estilos() {
	add_action('widgets_init', create_function('', 'return register_widget("Destacados_Widget");') );
}

class Destacados_Widget extends WP_Widget {
	function Destacados_Widget() {
		$this->WP_Widget( false, 'Destacados Widget', array( 'description' => 'Elementos destacados de RosarioAlToque'));
		$this->default_num_to_show = 10;
		$this->default_destacados = "blogs";
	}

	function __construct() {
		$widget_ops = array( 'description' => 'Conjuntos de elementos destacados del sitio.');
		parent::__construct( false, 'Destacados', $widget_ops );
		
    if ( is_active_widget( false, false, $this->id_base ) ) {
      wp_enqueue_script( 'jcarousel-for-widgets-js', get_template_directory_uri(). '/js/jquery.jcarousel.js', array( 'jquery' ) );
      wp_enqueue_script( 'jTabs-for-widgets-js', get_template_directory_uri(). '/js/jquery.idTabs.min.js', array( 'jquery' ) );
		}
	}

	function form( $instance ) {
		$title = ( isset( $instance['title'] ) ) ? esc_attr( $instance['title'] ) : '';
		$title_id = $this->get_field_id( 'title' );
		$title_name = $this->get_field_name( 'title' );
		
    $num_to_show = ( isset( $instance['num_to_show'] ) ) ? esc_attr( $instance['num_to_show'] ) : $this->default_num_to_show;
		$num_to_show_id = $this->get_field_id( 'num_to_show' );
		$num_to_show_name = $this->get_field_name( 'num_to_show' );
    
    $destacados = ( isset( $instance['destacados']))? $instance['destacados'] : $this->default_destacados; //esc_attr( $instance['destacados'])
    $destacados_id = $this->get_field_id('destacados');
		$destacados_name = $this->get_field_name( 'destacados');

?>
	
  <p>
		<label for="<?php echo $title_id ?>"><?php _e( 'Title:', 'p2' ); ?>
			<input type="text" class="widefat" id="<?php echo $title_id ?>" name="<?php echo $title_name ?>"
				value="<?php echo $title; ?>" />
		</label>
	</p>
	<p>
		<label for="<?php echo $destacados_id ?>">
      Destacados:
      <textarea id="<?php echo $this->get_field_id('destacados'); ?>" name="<?php echo $this->get_field_name('destacados'); ?>" rows="28" cols="14"><?php echo $instance['destacados']; ?></textarea>
      
		</label>
	</p>
	<p>
		<label for="<?php echo $num_to_show_id ?>"><?php _e( 'Number of tags to show:', 'p2' ); ?>
			<input type="text" class="widefat" id="<?php echo $num_to_show_id ?>" name="<?php echo $num_to_show_name ?>"
				value="<?php echo $num_to_show; ?>" />
		</label>
	</p>
<?php
	}

  function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		$instance['title'] = strip_tags($new_instance['title']);
    $instance['num_to_show'] = (int)$new_instance['num_to_show']? (int)$new_instance['num_to_show'] : $this->default_num_to_show;
    
    error_log(var_dump($old_instance));
    error_log(var_dump($new_instance));
    
    $instance['destacados'] = $new_instance['destacados'];
    
		return $instance;
	}  

	function widget( $args, $instance ) {
		extract( $args );

		$title = (isset( $instance['title'] ) && $instance['title'])? $instance['title'] : __( 'Recent tags', 'p2' );
		$num_to_show = (isset( $instance['num_to_show'] ) && (int)$instance['num_to_show'])? (int)$instance['num_to_show'] : $this->default_num_to_show;
    $destacados = $instance['destacados']; // Lista de destacados
    
    $arr_destacados = explode("\r", $destacados);
    
    foreach ($arr_destacados as $destcds ){
#      echo "Destacado: ".$destcds."<BR>";
    }
    
    /*
      Que hay que hacer:
        En el control hay que agregar campos de texto para poner una lista de urls o slugs destacados.
    */
    
    echo $before_widget;
    echo $before_title
      . $instance['title']
      . $after_title;


    $search_args = array(
      'child_of'=> get_cat_ID('blogs'),
      'orderby' => 'name',
      'order'   => 'ASC'
    );
    $categories = get_categories($search_args);
    ?>
      <script type="text/javascript">
        jQuery(document).ready(function() {
          jQuery('#carusel-destacados').jcarousel();
        });
      </script>
      

      <div id="marco" class="destacados">
        <div id="uno" class="detalle">
          <div class="foto">
            <img src="/wp-content/themes/rcd/images/destacados/uno_grande.jpg" >
          </div>
          <div class="descripcion">
            <div class="titulo"><h3>Titulo 1</h3></div>
            <div class="texto">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed ac elit. Cras tellus leo, sollicitudin a, sagittis eu, vulputate quis, arcu. Proin at diam. Pellentesque sodales rhoncus purus.</div>
            <div class="social"></div>
          </div>
        </div>
        <div id="dos" class="detalle" style="display: none">
          <div class="foto">
            <img src="/wp-content/themes/rcd/images/destacados/dos_grande.jpg" >
          </div>
          <div class="descripcion">
            <div class="titulo"><h3>Titulo 2</h3></div>
            <div class="texto">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed ac elit. Cras tellus leo, sollicitudin a, sagittis eu, vulputate quis, arcu. Proin at diam. Pellentesque sodales rhoncus purus.</div>
            <div class="social"></div>
          </div>
        </div>
        <div id="tres" class="detalle" style="display: none">
          <div class="foto">
            <img src="/wp-content/themes/rcd/images/destacados/tres_grande.jpg" >
          </div>
          <div class="descripcion">
            <div class="titulo"><h3>Titulo 3</h3></div>
            <div class="texto">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed ac elit. Cras tellus leo, sollicitudin a, sagittis eu, vulputate quis, arcu. Proin at diam. Pellentesque sodales rhoncus purus.</div>
            <div class="social"></div>
          </div>
        </div>
        <div id="cuatro" class="detalle" style="display: none">
          <div class="foto">
            <img src="/wp-content/themes/rcd/images/destacados/cuatro_grande.jpg" >
          </div>
          <div class="descripcion">
            <div class="titulo"><h3>Titulo 4</h3></div>
            <div class="texto">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed ac elit. Cras tellus leo, sollicitudin a, sagittis eu, vulputate quis, arcu. Proin at diam. Pellentesque sodales rhoncus purus.</div>
            <div class="social"></div>
          </div>
        </div>
        <div id="cinco" class="detalle" style="display: none">
          <div class="foto">
            <img src="/wp-content/themes/rcd/images/destacados/cinco_grande.jpg" >
          </div>
          <div class="descripcion">
            <div class="titulo"><h3>Titulo 5</h3></div>
            <div class="texto">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed ac elit. Cras tellus leo, sollicitudin a, sagittis eu, vulputate quis, arcu. Proin at diam. Pellentesque sodales rhoncus purus.</div>
            <div class="social"></div>
          </div>
        </div>


        
        <div id="tira" class="destacados-tira">
          <ul id="carusel-destacados" class="idTabs destacados-skin">
            <li><div class="miniatura"><a href="#uno">
              <img src="/wp-content/themes/rcd/images/destacados/uno.jpg"/>
            </a></div></li>
            <li><div class="miniatura"><a href="#dos">
              <img src="/wp-content/themes/rcd/images/destacados/dos.jpg"/>
            </a></div></li>
            <li><div class="miniatura"><a href="#tres">
              <img src="/wp-content/themes/rcd/images/destacados/tres.jpg"/>
            </a></div></li>
            <li><div class="miniatura"><a href="#cuatro">
              <img src="/wp-content/themes/rcd/images/destacados/cuatro.jpg"/>
            </a></div></li>
            <li><div class="miniatura"><a href="#cinco">
              <img src="/wp-content/themes/rcd/images/destacados/cinco.jpg"/>
            </a></div></li>
            
            <?php
            /*
            foreach ($categories as $category) {
              ?> <li> <?php
              if (function_exists('get_terms_meta')) {
                $metaValue = get_terms_meta($category->term_id, 'imagen' );
                echo '<div class="miniatura" >'.
                  '<a href="'.get_category_link($category->term_id).'" title="' . sprintf( __( "View all posts in %s" ), $category->name).'" '.'>'
                .'<img width="50" height="50" title="" alt="" src="'.$metaValue[0].'">'
                .'</div></a>';
              }
              else {
                error_log("NO EXISTE LA FUNCION ! ! ! ! ! ! !");
              }
              echo '<div class="item">'.'<div class="item-title">'.$category->name. '</div>';
              echo '<div style="overflow: hidden; height: 58px;">'.implode(' ', array_slice(explode(' ', $category->description), 0, 25)) .' [...]'   . '</div>';
              echo '<span class="usuarios_widget">'. $category->count . ' entradas.</span>';

              echo '</div>';
              echo '</li>';
            }
            foreach ($categories as $category) {
              ?> <li> <?php
              if (function_exists('get_terms_meta')) {
                $metaValue = get_terms_meta($category->term_id, 'imagen' );
                echo '<div class="miniatura" >'.
                  '<a href="'.get_category_link($category->term_id).'" title="' . sprintf( __( "View all posts in %s" ), $category->name).'" '.'>'
                .'<img width="50" height="50" title="" alt="" src="'.$metaValue[0].'">'
                .'</div></a>';
              }
              else {
                error_log("NO EXISTE LA FUNCION ! ! ! ! ! ! !");
              }
              echo '<div class="item">'.'<div class="item-title">'.$category->name. '</div>';
              echo '<div style="overflow: hidden; height: 58px;">'.implode(' ', array_slice(explode(' ', $category->description), 0, 25)) .' [...]'   . '</div>';
              echo '<span class="usuarios_widget">'. $category->count . ' entradas.</span>';

              echo '</div>';
              echo '</li>';
            }
            */
            ?>
          </ul>        
        
        
        
        
        </div>
      </div>
<?php
    echo $after_widget;
    wp_reset_postdata();
	}
}
/*
      <ul id="widget-destacados" class="jcarousel-skin-tango" style="width: 1000px;">
        <?php
        foreach ($categories as $category) {
          ?> <li style="float: right;"> <?php
          if (function_exists('get_terms_meta')) {
            $metaValue = get_terms_meta($category->term_id, 'imagen');

            echo '<div class="item-avatar" >'.
              '<a href="'.get_category_link($category->term_id).'" title="' . sprintf( __( "View all posts in %s" ), $category->name).'" '.'>'
            .'<img width="254" height="254" title="" alt="" src="'.$metaValue[0].'">'
            .'</div></a>';
          }
          else {
            error_log("NO EXISTE LA FUNCION ! ! ! ! ! ! !");
          }
          echo '<div class="item">'.'<div class="item-title">'.$category->name. '</div>';
          echo '<div style="overflow: hidden; height: 58px;">'.implode(' ', array_slice(explode(' ', $category->description), 0, 25)) .' [...]'   . '</div>';
          echo '<span class="usuarios_widget">'. $category->count . ' entradas.</span>';

          echo '</div>';
          echo '</li>';
        }
        ?>
    </ul>


*/
?>

